﻿using MPic.Business.DAL;
using MPic.Business.Entity;
using MPic.Business.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MPic.Business.BLL
{
    public class AliYunOSSBLL
    {
        private AliYunOSSDAL aliYunOSSDal = new AliYunOSSDAL(DbConnectionEnum.MPic);
        private static object locker = new object();
        private static AliYunOSSBLL _instance;
        public static AliYunOSSBLL Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new AliYunOSSBLL();
                        }
                    }
                }
                return _instance;
            }
        }
        public AliYunOSSEntity GetById(int ossId)
        {
            return aliYunOSSDal.Get(ossId);
        }

        public AliYunOSSEntity GetByUserId(int userId)
        {
            return aliYunOSSDal.Get(new { UserId = userId }, "OssId DESC");
        }

        /// <summary>
        /// 新增阿里云OSS配置
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="bucket"></param>
        /// <param name="accessKey"></param>
        /// <param name="secretKey"></param>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool Insert(int userId, string bucket, string endpoint, string accessKey, string secretKey)
        {
            var oss = new AliYunOSSEntity
            {
                UserId = userId,
                Bucket = bucket,
                AccessKey = accessKey,
                SecretKey = secretKey,
                Endpoint = endpoint,
                CreateTime = DateTime.Now,
                Remark = string.Empty
            };
            return aliYunOSSDal.Insert(oss) > 0;
        }

        /// <summary>
        /// 修改阿里云OSS配置
        /// </summary>
        /// <param name="qiniu"></param>
        /// <returns></returns>
        public bool Update(AliYunOSSEntity oss)
        {
            return aliYunOSSDal.Update(oss) > 0;
        }
    }
}
